package com.gmics.unifiq.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.util.Log;

import com.gmics.unifiq.entity.Avatar;
import com.gmics.unifiq.entity.CheckIn;
import com.gmics.unifiq.entity.Event;
import com.gmics.unifiq.entity.Group;
import com.gmics.unifiq.entity.Notification;
import com.gmics.unifiq.entity.OpenEvent;
import com.gmics.unifiq.entity.User;
import com.gmics.unifiq.entity.UserNotification;

@SuppressLint("SimpleDateFormat")
public class EntityController {
	private JSONController jController = new JSONController();
	private static String retrieve_open_event_url = "http://unifiq.5kportal.com/retrieve_open_event.php";
	private static String retrieve_open_event_by_id_url = "http://unifiq.5kportal.com/retrieve_open_event_by_id.php";
	private static String create_event_url = "http://unifiq.5kportal.com/create_event.php";
	private static String retrieve_event_url = "http://unifiq.5kportal.com/retrieve_event.php";
	private static String retrieve_group_url = "http://unifiq.5kportal.com/retrieve_group.php";
	private static String retrieve_user_group_id_url = "http://unifiq.5kportal.com/retrieve_user_group_id.php";
	private static String retrieve_group_user_id_url = "http://unifiq.5kportal.com/retrieve_group_user_id.php";
	private static String retrieve_checkin_url = "http://unifiq.5kportal.com/retrieve_checkin.php";
	private static String create_checkin_url = "http://unifiq.5kportal.com/create_checkin.php";
	private static String retrieve_open_event_attendance_url = "http://unifiq.5kportal.com/retrieve_open_event_attendance.php";
	private static String create_open_event_attendance_url = "http://unifiq.5kportal.com/create_open_event_attendance.php";
	private static String retrieve_notification_url = "http://unifiq.5kportal.com/retrieve_notification.php";
	private static String retrieve_user_url = "http://unifiq.5kportal.com/retrieve_user.php";
	private static String create_user_group_url = "http://unifiq.5kportal.com/create_user_group.php";
	private static String retrieve_user_by_id_url = "http://unifiq.5kportal.com/retrieve_user_by_id.php";
	private static String update_event_url = "http://unifiq.5kportal.com/update_event.php";
	private static String retrieve_avatar_url = "http://unifiq.5kportal.com/retrieve_avatar.php";
	private static String retrieve_avatar_by_id_url = "http://unifiq.5kportal.com/retrieve_avatar_by_id.php";
	private static String update_user_avatar_url = "http://unifiq.5kportal.com/update_user_avatar.php";
	private static String create_notification_url = "http://unifiq.5kportal.com/create_notification.php";
	private static String create_user_notification_url = "http://unifiq.5kportal.com/create_user_notification.php";
	private static String retrieve_user_notification_url = "http://unifiq.5kportal.com/retrieve_user_notification.php";
	private static String update_user_notification_url = "http://unifiq.5kportal.com/update_user_notification.php";
	private static String retrieve_avatar_filter_url = "http://unifiq.5kportal.com/retrieve_avatar_filter.php";

	private static final String TAG_SUCCESS = "success", TAG_OPEN_EVENT = "oe",
			TAG_ID = "id", TAG_NAME = "name", TAG_DATE_TIME = "dateTime",
			TAG_LOCATION = "location", TAG_CREATION_DATE = "creationDate",
			TAG_CREATOR_ID = "creatorID", TAG_LATITUDE = "latitude",
			TAG_LONGITUDE = "longitude", TAG_GROUP_ID = "groupID",
			TAG_EVENT = "event", TAG_GROUP = "group",
			TAG_IMAGE_PATH = "imagePath", TAG_USER_ID = "userID",
			TAG_GROUP_USERS = "groupUsers",
			TAG_LOCATION_REF = "locationReference", TAG_TYPE = "type",
			TAG_DESC = "description", TAG_CONDITION = "condition",
			TAG_EVENT_ID = "eventID", TAG_CHECKIN = "checkin",
			TAG_ADMIN_ID = "adminID", TAG_TYPE_ID = "typeID",
			TAG_CAPACITY = "capacity", TAG_SUBMISSION_DATE = "submissionDate",
			TAG_OPEN_EVENT_ID = "openEventID",
			TAG_OPEN_EVENT_ATTENDANCE = "oeAttendance",
			TAG_NOTIFICATION = "notification", TAG_UNIQUE_ID = "unique_id",
			TAG_USER = "user", TAG_UID = "uid", TAG_CATEGORY_ID = "categoryID",
			TAG_ICON_PATH = "iconPath", TAG_AVATAR = "avatar",
			TAG_AVATAR_ID = "avatarID", TAG_ORIGINAL = "original",
			TAG_UPDATE = "update", TAG_NOTIFICATION_ID = "notificationID",
			TAG_READ = "read", TAG_USER_NOTIFICATION = "userNotification",
			TAG_REQUIREMENT = "requirement", TAG_EMAIL = "email",
			TAG_BIRTHDAY = "birthday", TAG_SCHOOL = "school", TAG_PROFILEPIC = "profilePic";

	private final static SimpleDateFormat DATE_TIME_SDF = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat DATE_SDF = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static final SimpleDateFormat TIME_SDF = new SimpleDateFormat(
			"HH:mm");

	public static String convertDateTimeToFullString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy @ h:mm a");
		return sdf.format(date);
	}

	public static String convertDateTimeToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
		return sdf.format(date);
	}

	public static String dateTimeToString(Date date) {
		return DATE_TIME_SDF.format(date);
	}

	public static Date dateTimeToDate(Date date) {
		try {
			return DATE_SDF.parse(DATE_SDF.format(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static Date dateTimeToTime(Date time) {
		try {
			return TIME_SDF.parse(TIME_SDF.format(time));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String dateToString(Date date) {
		return DATE_SDF.format(date);
	}

	public static Date stringToDate(String date) throws ParseException {
		return DATE_SDF.parse(date);
	}

	public static Date stringToDateTime(String date) throws ParseException {
		return DATE_TIME_SDF.parse(date);
	}

	public static Date stringToTime(String time) throws ParseException {
		return TIME_SDF.parse(time);
	}

	public static String timeToString(Date time) {
		return TIME_SDF.format(time);
	}

	public boolean createEvent(int creatorID, String name, Date date,
			String time, int groupID, String locationRef, String desc) {

		String dateTime = dateToString(date) + " " + time;
		List<NameValuePair> eventParams = new ArrayList<NameValuePair>();
		eventParams.add(new BasicNameValuePair(TAG_CREATION_DATE,
				dateToString(Calendar.getInstance().getTime())));
		eventParams.add(new BasicNameValuePair(TAG_CREATOR_ID, String
				.valueOf(creatorID)));
		eventParams.add(new BasicNameValuePair(TAG_NAME, name));
		eventParams.add(new BasicNameValuePair(TAG_DATE_TIME, dateTime));
		eventParams.add(new BasicNameValuePair(TAG_GROUP_ID, String
				.valueOf(groupID)));
		eventParams.add(new BasicNameValuePair(TAG_LOCATION_REF, locationRef));
		eventParams.add(new BasicNameValuePair(TAG_DESC, desc));

		JSONObject json = jController.makeHttpRequest(create_event_url, "POST",
				eventParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public ArrayList<CheckIn> retrieveCheckIn(int userID) {
		ArrayList<CheckIn> checkInArr = new ArrayList<CheckIn>();
		JSONArray checkInJArr = null;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add((new BasicNameValuePair(TAG_CONDITION, "WHERE userID = "
				+ String.valueOf(userID))));
		JSONObject json = jController.makeHttpRequest(retrieve_checkin_url,
				"GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				checkInJArr = json.getJSONArray(TAG_CHECKIN);

				for (int i = 0; i < checkInJArr.length(); i++) {
					JSONObject jObject = checkInJArr.getJSONObject(i);

					CheckIn checkIn = new CheckIn();
					checkIn.setUserID(jObject.getInt(TAG_USER_ID));
					checkIn.setOpenEventID(jObject.getInt(TAG_OPEN_EVENT_ID));
					checkIn.setEventID(jObject.getInt(TAG_EVENT_ID));

					checkInArr.add(checkIn);
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return checkInArr;
	}

	public ArrayList<Event> retrieveEventByID(ArrayList<Integer> eventIDArr) {
		ArrayList<Event> eventArr = new ArrayList<Event>();

		for (int a = 0; a < eventIDArr.size(); a++) {
			JSONArray eventJArr = null;
			List<NameValuePair> eventParams = new ArrayList<NameValuePair>();
			eventParams.add((new BasicNameValuePair(TAG_CONDITION,
					"WHERE id = " + String.valueOf(eventIDArr.get(a))
							+ " ORDER BY dateTime")));
			JSONObject eventJSon = jController.makeHttpRequest(
					retrieve_event_url, "GET", eventParams);

			try {
				int success = eventJSon.getInt(TAG_SUCCESS);
				Log.d("json", eventJSon.toString());
				if (success == 1) {
					eventJArr = eventJSon.getJSONArray(TAG_EVENT);

					for (int i = 0; i < eventJArr.length(); i++) {
						JSONObject jObject = eventJArr.getJSONObject(i);

						int id = jObject.getInt(TAG_ID);
						int creatorID = jObject.getInt(TAG_CREATOR_ID);
						String name = jObject.getString(TAG_NAME);
						int groupID = jObject.getInt(TAG_GROUP_ID);
						String locationRef = jObject
								.getString(TAG_LOCATION_REF);
						String desc = jObject.getString(TAG_DESC);

						Date creationDate = null, dateTime = null;

						try {
							creationDate = stringToDate(jObject
									.getString(TAG_CREATION_DATE));
							dateTime = stringToDateTime(jObject
									.getString(TAG_DATE_TIME));
						} catch (ParseException e1) {
							e1.printStackTrace();
						}

						Event event = new Event();
						event.setId(id);
						event.setCreationDate(creationDate);
						event.setCreatorID(creatorID);
						event.setName(name);
						event.setGroupID(groupID);
						event.setLocationReference(locationRef);
						event.setDescription(desc);

						if (creationDate != null) {
							event.setCreationDate(creationDate);
						}

						if (dateTime != null) {
							event.setDateTime(dateTime);
						}

						eventArr.add(event);

					}
				} else {
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return eventArr;
	}

	public Event retrieveEventByID(int eventID) {
		Event event = new Event();

		JSONArray eventJArr = null;
		List<NameValuePair> eventParams = new ArrayList<NameValuePair>();
		eventParams.add((new BasicNameValuePair(TAG_CONDITION, "WHERE id = "
				+ String.valueOf(eventID))));
		JSONObject eventJSon = jController.makeHttpRequest(retrieve_event_url,
				"GET", eventParams);

		try {
			int success = eventJSon.getInt(TAG_SUCCESS);
			Log.d("json", eventJSon.toString());
			if (success == 1) {
				eventJArr = eventJSon.getJSONArray(TAG_EVENT);

				for (int i = 0; i < eventJArr.length(); i++) {
					JSONObject jObject = eventJArr.getJSONObject(i);

					int id = jObject.getInt(TAG_ID);
					int creatorID = jObject.getInt(TAG_CREATOR_ID);
					String name = jObject.getString(TAG_NAME);
					int groupID = jObject.getInt(TAG_GROUP_ID);
					String locationRef = jObject.getString(TAG_LOCATION_REF);
					String desc = jObject.getString(TAG_DESC);

					Date creationDate = null, dateTime = null;

					try {
						creationDate = stringToDate(jObject
								.getString(TAG_CREATION_DATE));
						dateTime = stringToDateTime(jObject
								.getString(TAG_DATE_TIME));
					} catch (ParseException e1) {
						e1.printStackTrace();
					}

					event.setId(id);
					event.setCreationDate(creationDate);
					event.setCreatorID(creatorID);
					event.setName(name);
					event.setGroupID(groupID);
					event.setLocationReference(locationRef);
					event.setDescription(desc);

					if (creationDate != null) {
						event.setCreationDate(creationDate);
					}

					if (dateTime != null) {
						event.setDateTime(dateTime);
					}

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return event;
	}

	public ArrayList<Event> retrieveEventByGroupID(ArrayList<Integer> grpIDArr) {
		ArrayList<Event> eventArr = new ArrayList<Event>();

		for (int a = 0; a < grpIDArr.size(); a++) {
			JSONArray eventJArr = null;
			List<NameValuePair> eventParams = new ArrayList<NameValuePair>();
			eventParams.add((new BasicNameValuePair(TAG_CONDITION,
					"WHERE groupID = " + String.valueOf(grpIDArr.get(a))
							+ " ORDER BY dateTime")));
			JSONObject eventJSon = jController.makeHttpRequest(
					retrieve_event_url, "GET", eventParams);

			try {
				int success = eventJSon.getInt(TAG_SUCCESS);

				if (success == 1) {
					eventJArr = eventJSon.getJSONArray(TAG_EVENT);

					for (int i = 0; i < eventJArr.length(); i++) {
						JSONObject jObject = eventJArr.getJSONObject(i);

						int id = jObject.getInt(TAG_ID);
						int creatorID = jObject.getInt(TAG_CREATOR_ID);
						String name = jObject.getString(TAG_NAME);
						int groupID = jObject.getInt(TAG_GROUP_ID);
						String locationRef = jObject
								.getString(TAG_LOCATION_REF);
						String desc = jObject.getString(TAG_DESC);

						Date creationDate = null, dateTime = null;

						try {
							creationDate = stringToDate(jObject
									.getString(TAG_CREATION_DATE));
							dateTime = stringToDateTime(jObject
									.getString(TAG_DATE_TIME));
						} catch (ParseException e1) {
							e1.printStackTrace();
						}

						Event event = new Event();
						event.setId(id);
						event.setCreationDate(creationDate);
						event.setCreatorID(creatorID);
						event.setName(name);
						event.setGroupID(groupID);
						event.setLocationReference(locationRef);
						event.setDescription(desc);

						if (creationDate != null) {
							event.setCreationDate(creationDate);
						}

						if (dateTime != null) {
							event.setDateTime(dateTime);
						}

						eventArr.add(event);

					}
				} else {
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return eventArr;
	}

	public ArrayList<Group> retrieveGroup(ArrayList<Integer> grpIDArr) {
		ArrayList<Group> grpArr = new ArrayList<Group>();

		for (int a = 0; a < grpIDArr.size(); a++) {
			grpArr.add(retrieveGroup(grpIDArr.get(a)));
		}

		return grpArr;
	}

	public Group retrieveGroup(int grpID) {
		Group grp = new Group();

		JSONArray grpJArr = null;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_GROUP_ID, String.valueOf(grpID)));
		JSONObject json = jController.makeHttpRequest(retrieve_group_url,
				"GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				grpJArr = json.getJSONArray(TAG_GROUP);

				for (int i = 0; i < grpJArr.length(); i++) {
					JSONObject jObject = grpJArr.getJSONObject(i);

					int id = jObject.getInt(TAG_GROUP_ID);
					String name = jObject.getString(TAG_NAME);
					String type = jObject.getString(TAG_TYPE);
					String desc = jObject.getString(TAG_DESC);
					String url = jObject.getString(TAG_IMAGE_PATH);

					grp.setGroupID(id);
					grp.setName(name);
					grp.setDescription(desc);
					grp.setImagePath(url);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return grp;
	}

	public Event retrieveLatestEvent() {
		Event event = new Event();
		JSONArray eventJArr = null;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add((new BasicNameValuePair(TAG_CONDITION,
				"ORDER BY id DESC LIMIT 1")));
		JSONObject json = jController.makeHttpRequest(retrieve_event_url,
				"GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				eventJArr = json.getJSONArray(TAG_EVENT);

				for (int i = 0; i < eventJArr.length(); i++) {
					JSONObject jObject = eventJArr.getJSONObject(i);

					int id = jObject.getInt(TAG_ID);
					int creatorID = jObject.getInt(TAG_CREATOR_ID);
					String name = jObject.getString(TAG_NAME);
					int groupID = jObject.getInt(TAG_GROUP_ID);
					String locationRef = jObject.getString(TAG_LOCATION_REF);
					String desc = jObject.getString(TAG_DESC);

					Date creationDate = null, dateTime = null;

					try {
						creationDate = stringToDate(jObject
								.getString(TAG_CREATION_DATE));
						dateTime = stringToDateTime(jObject
								.getString(TAG_DATE_TIME));
					} catch (ParseException e1) {
						e1.printStackTrace();
					}

					event.setId(id);
					event.setCreationDate(creationDate);
					event.setCreatorID(creatorID);
					event.setName(name);
					event.setGroupID(groupID);
					event.setLocationReference(locationRef);
					event.setDescription(desc);

					if (creationDate != null) {
						event.setCreationDate(creationDate);
					}

					if (dateTime != null) {
						event.setDateTime(dateTime);
					}

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return event;
	}

	public ArrayList<OpenEvent> retrieveOpenEventByID(ArrayList<Integer> oeIDArr) {
		ArrayList<OpenEvent> oeArr = new ArrayList<OpenEvent>();

		for (int a = 0; a < oeIDArr.size(); a++) {
			oeArr.add(retrieveOpenEventByID(oeIDArr.get(a)));
		}

		return oeArr;
	}

	public OpenEvent retrieveOpenEventByID(int oeID) {
		OpenEvent openEvent = new OpenEvent();
		JSONArray openEventJArr = null;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add((new BasicNameValuePair(TAG_CONDITION, "WHERE id = "
				+ String.valueOf(oeID))));
		JSONObject json = jController.makeHttpRequest(
				retrieve_open_event_by_id_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				openEventJArr = json.getJSONArray(TAG_OPEN_EVENT);

				for (int i = 0; i < openEventJArr.length(); i++) {
					JSONObject jObject = openEventJArr.getJSONObject(i);

					int id = jObject.getInt(TAG_ID);
					int adminID = jObject.getInt(TAG_ADMIN_ID);
					int capacity = jObject.getInt(TAG_CAPACITY);
					String name = jObject.getString(TAG_NAME);
					String location = jObject.getString(TAG_LOCATION);
					double latitude = jObject.getDouble(TAG_LATITUDE);
					double longitude = jObject.getDouble(TAG_LONGITUDE);
					String desc = jObject.getString(TAG_DESC);

					Date dateTime = null, submissionDate = null;

					try {
						dateTime = stringToDateTime(jObject
								.getString(TAG_DATE_TIME));
						submissionDate = stringToDate(jObject
								.getString(TAG_SUBMISSION_DATE));
					} catch (ParseException e1) {
						e1.printStackTrace();
					}

					openEvent.setId(id);

					openEvent.setAdminID(adminID);
					openEvent.setName(name);
					openEvent.setCapacity(capacity);
					openEvent.setLocation(location);
					openEvent.setLatitude(latitude);
					openEvent.setLongitude(longitude);
					openEvent.setDescription(desc);

					if (dateTime != null) {
						openEvent.setDateTime(dateTime);
					}

					if (submissionDate != null) {
						openEvent.setSubmissionDate(submissionDate);
					}

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return openEvent;
	}

	public ArrayList<OpenEvent> retrieveOpenEvent() {
		ArrayList<OpenEvent> oeArr = new ArrayList<OpenEvent>();
		JSONArray openEventJArr = null;

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		JSONObject json = jController.makeHttpRequest(retrieve_open_event_url,
				"GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				openEventJArr = json.getJSONArray(TAG_OPEN_EVENT);

				for (int i = 0; i < openEventJArr.length(); i++) {
					JSONObject jObject = openEventJArr.getJSONObject(i);

					int id = jObject.getInt(TAG_ID);
					int adminID = jObject.getInt(TAG_ADMIN_ID);
					int capacity = jObject.getInt(TAG_CAPACITY);
					String name = jObject.getString(TAG_NAME);
					String location = jObject.getString(TAG_LOCATION);
					double latitude = jObject.getDouble(TAG_LATITUDE);
					double longitude = jObject.getDouble(TAG_LONGITUDE);
					String desc = jObject.getString(TAG_DESC);

					Date dateTime = null, submissionDate = null;

					try {
						dateTime = stringToDateTime(jObject
								.getString(TAG_DATE_TIME));
						submissionDate = stringToDate(jObject
								.getString(TAG_SUBMISSION_DATE));
					} catch (ParseException e1) {
						e1.printStackTrace();
					}

					OpenEvent openEvent = new OpenEvent();
					openEvent.setId(id);

					openEvent.setAdminID(adminID);
					openEvent.setName(name);
					openEvent.setCapacity(capacity);
					openEvent.setLocation(location);
					openEvent.setLatitude(latitude);
					openEvent.setLongitude(longitude);
					openEvent.setDescription(desc);

					if (dateTime != null) {
						openEvent.setDateTime(dateTime);
					}

					if (submissionDate != null) {
						openEvent.setSubmissionDate(submissionDate);
					}

					oeArr.add(openEvent);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return oeArr;
	}

	public ArrayList<Integer> retrieveUserGroupID(int userID) {
		ArrayList<Integer> grpIDArr = new ArrayList<Integer>();

		JSONArray grpJArr = null;
		List<NameValuePair> groupUsersParams = new ArrayList<NameValuePair>();
		groupUsersParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_user_group_id_url, "GET", groupUsersParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				grpJArr = groupUsersJSon.getJSONArray(TAG_GROUP_USERS);

				for (int i = 0; i < grpJArr.length(); i++) {
					JSONObject jObject = grpJArr.getJSONObject(i);
					grpIDArr.add(jObject.getInt(TAG_GROUP_ID));

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return grpIDArr;
	}

	public ArrayList<Integer> retrieveGroupUserID(int groupID) {
		ArrayList<Integer> userIDArr = new ArrayList<Integer>();

		JSONArray grpJArr = null;
		List<NameValuePair> groupUsersParams = new ArrayList<NameValuePair>();
		groupUsersParams.add(new BasicNameValuePair(TAG_GROUP_ID, String
				.valueOf(groupID)));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_group_user_id_url, "GET", groupUsersParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				grpJArr = groupUsersJSon.getJSONArray(TAG_GROUP_USERS);

				for (int i = 0; i < grpJArr.length(); i++) {
					JSONObject jObject = grpJArr.getJSONObject(i);
					userIDArr.add(jObject.getInt(TAG_USER_ID));

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return userIDArr;
	}

	public boolean createCheckin(int userID, int openEventID, int eventID) {

		List<NameValuePair> checkinParams = new ArrayList<NameValuePair>();
		checkinParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		checkinParams.add(new BasicNameValuePair(TAG_OPEN_EVENT_ID, String
				.valueOf(openEventID)));
		checkinParams.add(new BasicNameValuePair(TAG_EVENT_ID, String
				.valueOf(eventID)));

		JSONObject json = jController.makeHttpRequest(create_checkin_url,
				"POST", checkinParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public ArrayList<ArrayList<Integer>> retrieveOpenEventAttendance(
			int openEventID, int userID) {
		ArrayList<ArrayList<Integer>> idArr = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> oeIDArr = new ArrayList<Integer>(), userIDArr = new ArrayList<Integer>();
		JSONArray openEventAttendanceJArr = null;

		String condition = "";

		if (openEventID != 0) {
			condition += "openEventID = " + String.valueOf(openEventID);
			if (userID != 0) {
				condition += " AND ";
			}
		}

		if (userID != 0) {
			condition += "userID = " + String.valueOf(userID);
		}

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add((new BasicNameValuePair(TAG_CONDITION, "WHERE " + condition)));
		JSONObject json = jController.makeHttpRequest(
				retrieve_open_event_attendance_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				openEventAttendanceJArr = json
						.getJSONArray(TAG_OPEN_EVENT_ATTENDANCE);

				for (int i = 0; i < openEventAttendanceJArr.length(); i++) {
					JSONObject jObject = openEventAttendanceJArr
							.getJSONObject(i);

					oeIDArr.add(jObject.getInt(TAG_OPEN_EVENT_ID));
					userIDArr.add(jObject.getInt(TAG_USER_ID));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		idArr.add(oeIDArr);
		idArr.add(userIDArr);
		return idArr;
	}

	public boolean createOpenEventAttendance(int openEventID, int userID) {

		List<NameValuePair> attendanceParams = new ArrayList<NameValuePair>();
		attendanceParams.add(new BasicNameValuePair(TAG_OPEN_EVENT_ID, String
				.valueOf(openEventID)));
		attendanceParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));

		JSONObject json = jController.makeHttpRequest(
				create_open_event_attendance_url, "POST", attendanceParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public Notification retrieveLatestOpenEventNotification() {
		Notification n = new Notification();
		JSONArray nJArr = null;

		String condition = " WHERE openEventID != 0 ORDER BY creationDate DESC LIMIT 1";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_CONDITION, condition));
		JSONObject json = jController.makeHttpRequest(
				retrieve_notification_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				nJArr = json.getJSONArray(TAG_NOTIFICATION);

				for (int i = 0; i < nJArr.length(); i++) {
					JSONObject jObject = nJArr.getJSONObject(i);

					n.setId(jObject.getInt(TAG_ID));
					try {
						n.setCreationDate(stringToDate(jObject
								.getString(TAG_CREATION_DATE)));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					n.setOpenEventID(jObject.getInt(TAG_OPEN_EVENT_ID));
					n.setEventID(jObject.getInt(TAG_EVENT_ID));
					n.setOriginal(jObject.getString(TAG_ORIGINAL));
					n.setUpdate(jObject.getString(TAG_UPDATE));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return n;
	}

	public Notification retrieveLatestNotification() {
		Notification n = new Notification();
		JSONArray nJArr = null;

		String condition = " ORDER BY id DESC LIMIT 1";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_CONDITION, condition));
		JSONObject json = jController.makeHttpRequest(
				retrieve_notification_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				nJArr = json.getJSONArray(TAG_NOTIFICATION);

				for (int i = 0; i < nJArr.length(); i++) {
					JSONObject jObject = nJArr.getJSONObject(i);

					n.setId(jObject.getInt(TAG_ID));
					try {
						n.setCreationDate(stringToDate(jObject
								.getString(TAG_CREATION_DATE)));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					n.setOpenEventID(jObject.getInt(TAG_OPEN_EVENT_ID));
					n.setEventID(jObject.getInt(TAG_EVENT_ID));
					n.setOriginal(jObject.getString(TAG_ORIGINAL));
					n.setUpdate(jObject.getString(TAG_UPDATE));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return n;
	}

	public ArrayList<Notification> retrieveNotification(
			ArrayList<Integer> oeIDArr, ArrayList<Integer> eventIDArr) {
		ArrayList<Notification> notificationArr = new ArrayList<Notification>();
		JSONArray nJArr = null;
		String condition = "WHERE ";

		for (int a = 0; a < oeIDArr.size(); a++) {
			if (a == 0) {
				condition += TAG_OPEN_EVENT_ID + " = "
						+ String.valueOf(oeIDArr.get(a));
			} else {
				condition += " OR " + TAG_OPEN_EVENT_ID + " = "
						+ String.valueOf(oeIDArr.get(a));
			}
		}

		for (int b = 0; b < eventIDArr.size(); b++) {
			if (oeIDArr.size() != 0) {
				condition += " OR " + TAG_EVENT_ID + " = "
						+ String.valueOf(eventIDArr.get(b));
			} else {
				if (b == 0) {
					condition += TAG_EVENT_ID + " = "
							+ String.valueOf(eventIDArr.get(b));
				}
			}
		}

		condition += " ORDER BY creationDate DESC LIMIT 10";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_CONDITION, condition));
		JSONObject json = jController.makeHttpRequest(
				retrieve_notification_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				nJArr = json.getJSONArray(TAG_NOTIFICATION);

				for (int i = 0; i < nJArr.length(); i++) {
					JSONObject jObject = nJArr.getJSONObject(i);

					Notification n = new Notification();

					n.setId(jObject.getInt(TAG_ID));
					try {
						n.setCreationDate(stringToDate(jObject
								.getString(TAG_CREATION_DATE)));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					n.setOpenEventID(jObject.getInt(TAG_OPEN_EVENT_ID));
					n.setEventID(jObject.getInt(TAG_EVENT_ID));
					n.setOriginal(jObject.getString(TAG_ORIGINAL));
					n.setUpdate(jObject.getString(TAG_UPDATE));

					notificationArr.add(n);
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return notificationArr;
	}

	public ArrayList<Notification> retrieveNotification(
			ArrayList<Integer> notificationIDArr) {
		ArrayList<Notification> notificationArr = new ArrayList<Notification>();
		JSONArray nJArr = null;
		String condition = "WHERE ";

		for (int a = 0; a < notificationIDArr.size(); a++) {
			if (a == 0) {
				condition += TAG_ID + " = "
						+ String.valueOf(notificationIDArr.get(a));
			} else {
				condition += " OR " + TAG_ID + " = "
						+ String.valueOf(notificationIDArr.get(a));
			}
		}

		condition += " ORDER BY creationDate DESC LIMIT 10";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_CONDITION, condition));
		JSONObject json = jController.makeHttpRequest(
				retrieve_notification_url, "GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				nJArr = json.getJSONArray(TAG_NOTIFICATION);

				for (int i = 0; i < nJArr.length(); i++) {
					JSONObject jObject = nJArr.getJSONObject(i);

					Notification n = new Notification();

					n.setId(jObject.getInt(TAG_ID));
					try {
						n.setCreationDate(stringToDate(jObject
								.getString(TAG_CREATION_DATE)));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					n.setOpenEventID(jObject.getInt(TAG_OPEN_EVENT_ID));
					n.setEventID(jObject.getInt(TAG_EVENT_ID));
					n.setOriginal(jObject.getString(TAG_ORIGINAL));
					n.setUpdate(jObject.getString(TAG_UPDATE));

					notificationArr.add(n);
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return notificationArr;
	}

	public boolean updateUserNotification(int userID) {

		List<NameValuePair> unParams = new ArrayList<NameValuePair>();
		unParams.add(new BasicNameValuePair(TAG_USER_ID, String.valueOf(userID)));
		unParams.add(new BasicNameValuePair(TAG_READ, String.valueOf(1)));

		JSONObject json = jController.makeHttpRequest(
				update_user_notification_url, "POST", unParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public User retrieveUser(String uniqueID) {
		User user = new User();

		JSONArray userJArr = null;
		List<NameValuePair> userParams = new ArrayList<NameValuePair>();
		userParams.add(new BasicNameValuePair(TAG_UNIQUE_ID, uniqueID));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_user_url, "GET", userParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				userJArr = groupUsersJSon.getJSONArray(TAG_USER);

				for (int i = 0; i < userJArr.length(); i++) {
					JSONObject jObject = userJArr.getJSONObject(i);

					user.setId(jObject.getInt(TAG_UID));
					user.setUniqueID(jObject.getString(TAG_UNIQUE_ID));
					user.setName(jObject.getString(TAG_NAME));
					user.setAvatarID(jObject.getInt(TAG_AVATAR_ID));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return user;
	}

	public User retrieveUser(int userID) {
		User user = new User();

		JSONArray userJArr = null;
		List<NameValuePair> userParams = new ArrayList<NameValuePair>();
		userParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		JSONObject groupUsersJSon = jController.makeHttpRequest(
				retrieve_user_by_id_url, "GET", userParams);

		try {
			int success = groupUsersJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				userJArr = groupUsersJSon.getJSONArray(TAG_USER);

				for (int i = 0; i < userJArr.length(); i++) {
					JSONObject jObject = userJArr.getJSONObject(i);

					user.setId(jObject.getInt(TAG_UID));
					user.setUniqueID(jObject.getString(TAG_UNIQUE_ID));
					user.setName(jObject.getString(TAG_NAME));
					user.setAvatarID(jObject.getInt(TAG_AVATAR_ID));
					user.setBirthday(jObject.getString(TAG_BIRTHDAY));
					user.setEmail(jObject.getString(TAG_EMAIL));
					user.setSchool(jObject.getString(TAG_SCHOOL));
					user.setProfilePic(jObject.getString(TAG_PROFILEPIC));
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return user;
	}

	public boolean createUserGroup(int userID, int groupID) {

		List<NameValuePair> userGroupParams = new ArrayList<NameValuePair>();
		userGroupParams.add(new BasicNameValuePair(TAG_GROUP_ID, String
				.valueOf(groupID)));
		userGroupParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));

		JSONObject json = jController.makeHttpRequest(create_user_group_url,
				"POST", userGroupParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean updateEvent(int eventID, String name, Date date,
			String time, String locationRef, String desc) {
		List<NameValuePair> eventParams = new ArrayList<NameValuePair>();

		String condition = "";
		ArrayList<String> conditionArr = new ArrayList<String>();

		if (name != null && !name.equals("")) {
			conditionArr.add(TAG_NAME + " = '" + name + "'");
		}
		if (date != null || time != null) {
			String dateTime = dateToString(date) + " " + time;
			conditionArr.add(TAG_DATE_TIME + " = '" + dateTime + "'");
		}
		if (locationRef != null) {
			conditionArr.add(TAG_LOCATION_REF + " = '" + locationRef + "'");
		}
		if (desc != null && !desc.equals("")) {
			conditionArr.add(TAG_DESC + " = '" + desc + "'");
		}

		for (int i = 0; i < conditionArr.size(); i++) {
			if (i == 0) {
				condition = conditionArr.get(i);
			} else {
				condition += " AND " + conditionArr.get(i);
			}
		}

		condition += " WHERE " + TAG_ID + " = " + String.valueOf(eventID);

		eventParams.add(new BasicNameValuePair(TAG_CONDITION, condition));
		JSONObject json = jController.makeHttpRequest(update_event_url, "POST",
				eventParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public ArrayList<Avatar> retrieveAvatar() {
		ArrayList<Avatar> avatarArr = new ArrayList<Avatar>();

		JSONArray avatarJArr = null;
		List<NameValuePair> avatarParams = new ArrayList<NameValuePair>();
		JSONObject avartarJSon = jController.makeHttpRequest(
				retrieve_avatar_url, "GET", avatarParams);

		try {
			int success = avartarJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				avatarJArr = avartarJSon.getJSONArray(TAG_AVATAR);

				for (int i = 0; i < avatarJArr.length(); i++) {
					JSONObject jObject = avatarJArr.getJSONObject(i);

					Avatar avatar = new Avatar();

					avatar.setId(jObject.getInt(TAG_ID));
					avatar.setCreatorID(jObject.getInt(TAG_CREATOR_ID));
					avatar.setCategoryID(jObject.getInt(TAG_CATEGORY_ID));
					avatar.setIconPath(jObject.getString(TAG_ICON_PATH));
					avatar.setImagePath(jObject.getString(TAG_IMAGE_PATH));
					avatar.setName(jObject.getString(TAG_NAME));
					avatar.setRequirement(jObject.getInt(TAG_REQUIREMENT));
					avatarArr.add(avatar);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return avatarArr;
	}

	public Avatar retrieveAvatar(int avatarID) {
		Avatar avatar = new Avatar();

		JSONArray avatarJArr = null;
		List<NameValuePair> avatarParams = new ArrayList<NameValuePair>();
		avatarParams.add(new BasicNameValuePair(TAG_ID, String
				.valueOf(avatarID)));
		JSONObject avartarJSon = jController.makeHttpRequest(
				retrieve_avatar_by_id_url, "GET", avatarParams);

		try {
			int success = avartarJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				avatarJArr = avartarJSon.getJSONArray(TAG_AVATAR);

				for (int i = 0; i < avatarJArr.length(); i++) {
					JSONObject jObject = avatarJArr.getJSONObject(i);

					avatar.setId(jObject.getInt(TAG_ID));
					avatar.setCreatorID(jObject.getInt(TAG_CREATOR_ID));
					avatar.setCategoryID(jObject.getInt(TAG_CATEGORY_ID));
					avatar.setIconPath(jObject.getString(TAG_ICON_PATH));
					avatar.setImagePath(jObject.getString(TAG_IMAGE_PATH));
					avatar.setName(jObject.getString(TAG_NAME));

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return avatar;
	}

	public ArrayList<Avatar> retrieveAvatarFilter(int requirement) {

		ArrayList<Avatar> avatarArr = new ArrayList<Avatar>();
		JSONArray avatarJArr = null;
		List<NameValuePair> avatarParams = new ArrayList<NameValuePair>();
		avatarParams.add(new BasicNameValuePair(TAG_REQUIREMENT, String
				.valueOf(requirement)));
		JSONObject avartarJSon = jController.makeHttpRequest(
				retrieve_avatar_filter_url, "GET", avatarParams);

		try {
			int success = avartarJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				avatarJArr = avartarJSon.getJSONArray(TAG_AVATAR);

				for (int i = 0; i < avatarJArr.length(); i++) {
					JSONObject jObject = avatarJArr.getJSONObject(i);

					Avatar avatar = new Avatar();

					avatar.setId(jObject.getInt(TAG_ID));
					avatar.setCreatorID(jObject.getInt(TAG_CREATOR_ID));
					avatar.setCategoryID(jObject.getInt(TAG_CATEGORY_ID));
					avatar.setIconPath(jObject.getString(TAG_ICON_PATH));
					avatar.setImagePath(jObject.getString(TAG_IMAGE_PATH));
					avatar.setName(jObject.getString(TAG_NAME));
					avatar.setRequirement(jObject.getInt(TAG_REQUIREMENT));
					avatarArr.add(avatar);
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return avatarArr;
	}

	public boolean updateUserAvatar(int userID, int avatarID) {

		List<NameValuePair> userAvatarParams = new ArrayList<NameValuePair>();
		userAvatarParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		userAvatarParams.add(new BasicNameValuePair(TAG_AVATAR_ID, String
				.valueOf(avatarID)));

		JSONObject json = jController.makeHttpRequest(update_user_avatar_url,
				"POST", userAvatarParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean createNotification(int openEventID, int eventID,
			ArrayList<String> originalArr, ArrayList<String> updatedArr) {
		String original = "", update = "";
		for (int i = 0; i < originalArr.size(); i++) {
			if (i == 0) {
				original = originalArr.get(i);
				update = updatedArr.get(i);
			} else {
				original += ", " + originalArr.get(i);
				update += ", " + updatedArr.get(i);
			}
		}

		return createNotification(openEventID, eventID, original, update);

	}

	public boolean createNotification(int openEventID, int eventID,
			String original, String update) {
		Date currentDate = EntityController.dateTimeToDate(Calendar
				.getInstance().getTime());

		List<NameValuePair> notificationParams = new ArrayList<NameValuePair>();
		notificationParams.add(new BasicNameValuePair(TAG_CREATION_DATE,
				dateToString(currentDate)));
		notificationParams.add(new BasicNameValuePair(TAG_OPEN_EVENT_ID, String
				.valueOf(openEventID)));
		notificationParams.add(new BasicNameValuePair(TAG_EVENT_ID, String
				.valueOf(eventID)));
		notificationParams.add(new BasicNameValuePair(TAG_ORIGINAL, original));
		notificationParams.add(new BasicNameValuePair(TAG_UPDATE, update));
		JSONObject json = jController.makeHttpRequest(create_notification_url,
				"POST", notificationParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean createUserNotification(int notificationID,
			ArrayList<Integer> userIDArr) {
		ArrayList<Boolean> successArr = new ArrayList<Boolean>();
		for (int i = 0; i < userIDArr.size(); i++) {
			successArr.add(createUserNotification(notificationID,
					userIDArr.get(i)));
		}

		for (int a = 0; a < successArr.size(); a++) {
			if (successArr.get(a) == false) {
				return false;
			}
		}

		return true;
	}

	public boolean createUserNotification(int notificationID, int userID) {
		List<NameValuePair> notificationParams = new ArrayList<NameValuePair>();
		notificationParams.add(new BasicNameValuePair(TAG_NOTIFICATION_ID,
				String.valueOf(notificationID)));
		notificationParams.add(new BasicNameValuePair(TAG_USER_ID, String
				.valueOf(userID)));
		JSONObject json = jController.makeHttpRequest(
				create_user_notification_url, "POST", notificationParams);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	public ArrayList<UserNotification> retrieveUserNotification(int userID) {
		ArrayList<UserNotification> unArr = new ArrayList<UserNotification>();

		JSONArray unJArr = null;
		List<NameValuePair> unParams = new ArrayList<NameValuePair>();
		unParams.add(new BasicNameValuePair(TAG_USER_ID, String.valueOf(userID)));
		JSONObject unJSon = jController.makeHttpRequest(
				retrieve_user_notification_url, "GET", unParams);

		try {
			int success = unJSon.getInt(TAG_SUCCESS);

			if (success == 1) {
				unJArr = unJSon.getJSONArray(TAG_USER_NOTIFICATION);

				for (int i = 0; i < unJArr.length(); i++) {
					JSONObject jObject = unJArr.getJSONObject(i);

					UserNotification un = new UserNotification();
					un.setNotificationID(jObject.getInt(TAG_NOTIFICATION_ID));
					un.setUserID(jObject.getInt(TAG_USER_ID));

					boolean read = false;
					if (jObject.getInt(TAG_READ) == 0) {
						read = false;
					} else if (jObject.getInt(TAG_READ) == 1) {
						read = true;
					}

					un.setRead(read);
					unArr.add(un);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return unArr;
	}
}
